import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.ImageObserver;

import javax.swing.*;
import java.net.*;

public class Mundo extends Applet implements Runnable,MouseListener,MouseMotionListener
{
    private Image dbImage;
    private Graphics dbg;
    private Image bGround;
    private Partes pc1;
    private Partes pc2;
    private Partes pc3;
    private Partes pc4;
    private Partes pc5;
    private Partes pc6;
    private Partes pc7;
    private Partes pc8;
    private Partes pc9;
    private Partes pc10;
    private Partes pc11;
    private Partes pc12;
    private Partes pc13;
    private Partes pc14;
    private Partes pc15;
    private Partes pc16;
    private Partes pc17;
    private Mapeamento mapCentral;
    private Botao btn;

    
    
    private Image pc1_img;
    private Image pc2_img;
    private Image pc3_img;
    private Image pc4_img;
    private Image pc5_img;
    private Image pc6_img;
    private Image pc7_img;
    private Image pc8_img;
    private Image pc9_img;
    private Image pc10_img;
    private Image pc11_img;
    private Image pc12_img;   
    private Image pc13_img;
    private Image pc14_img;
    private Image pc15_img;
    private Image pc16_img;
    private Image pc17_img;
    private Image sil1_img;
    private Image map_img;
    private Image btn_img;
    
    private GameState currentState;
    
    public Mundo()
    {
    	
    }
    
    public enum GameState
    {
    	stateStoped,
    	statePlaying;
    }
    
    public void setcurrentStatePlay()
    {
    	currentState = GameState.statePlaying;
    }
    
    public void setcurrentStateStop()
    {
    	currentState = GameState.stateStoped;
    }
    
	public void init()
	{
		this.setSize(760,420);
		MediaTracker mt = new MediaTracker(this);
		bGround = getImage(getCodeBase(),"castelo.gif");
		mt.addImage(bGround,0);
		try
		{
			mt.waitForID(0);
		}
		catch (InterruptedException ie)
		{
			//faz nda
		}
	
		
		mapCentral = new Mapeamento("peca1.gif",700, 20, "pc1", "peca1.gif","peca2.gif","peca3.gif","peca4.gif","peca5.gif","peca6.gif","peca7.gif","peca8.gif","peca9.gif","peca10.gif","peca11.gif","peca12.gif","peca13.gif","peca14.gif","peca15.gif","peca16.gif",this);
		map_img = getImage(getCodeBase(),mapCentral.getImagem());

        pc1 = new Partes("peca1.gif", "pc1", 45, 150, 43, 36, mapCentral);
        pc1_img = getImage(getCodeBase(), pc1.getImagem());
        pc2 = new Partes("peca2.gif", "pc2", 15, 200, 57, 95, mapCentral);
        pc2_img = getImage(getCodeBase(), pc2.getImagem());
        pc3 = new Partes("peca3.gif", "pc3", 65, 210, 38, 104, mapCentral);
        pc3_img = getImage(getCodeBase(), pc3.getImagem());
        pc4 = new Partes("peca4.gif", "pc4", 25, 290, 34, 101, mapCentral);
        pc4_img = getImage(getCodeBase(), pc4.getImagem());
        pc5 = new Partes("peca5.gif", "pc5", 120, 270, 37, 119, mapCentral);
        pc5_img = getImage(getCodeBase(), pc5.getImagem());
        pc6 = new Partes("peca6.gif", "pc6", 250, 30, 36, 127, mapCentral);
        pc6_img = getImage(getCodeBase(), pc6.getImagem());
        pc7 = new Partes("peca7.gif", "pc7", 150, 330, 36, 127, mapCentral);
        pc7_img = getImage(getCodeBase(), pc7.getImagem());
        pc8 = new Partes("peca8.gif", "pc8", 160, 170, 36, 127, mapCentral);
        pc8_img = getImage(getCodeBase(), pc8.getImagem());
        pc9 = new Partes("peca9.gif", "pc9", 210, 250, 36, 127, mapCentral);
        pc9_img = getImage(getCodeBase(), pc9.getImagem());
        pc10 = new Partes("peca10.gif", "pc10", 230, 100, 36, 127, mapCentral);
        pc10_img = getImage(getCodeBase(), pc10.getImagem());
        pc11 = new Partes("peca11.gif", "pc11", 370, 250, 36, 127, mapCentral);
        pc11_img = getImage(getCodeBase(), pc11.getImagem());
        pc12 = new Partes("peca12.gif", "pc12", 470, 290, 57, 95, mapCentral);
        pc12_img = getImage(getCodeBase(), pc12.getImagem());
        pc13 = new Partes("peca13.gif", "pc13", 430, 230, 38, 104, mapCentral);
        pc13_img = getImage(getCodeBase(), pc13.getImagem());
        pc14 = new Partes("peca14.gif", "pc14", 420, 130, 34, 101, mapCentral);
        pc14_img = getImage(getCodeBase(), pc14.getImagem());
        pc15 = new Partes("peca15.gif", "pc15", 290, 280, 37, 119, mapCentral);
        pc15_img = getImage(getCodeBase(), pc15.getImagem());
        pc16 = new Partes("peca16.gif", "p16", 280, 200, 36, 127, mapCentral);
        pc16_img = getImage(getCodeBase(), pc16.getImagem());
        pc17 = new Partes("peca17.gif", "pc17", 320, 140, 36, 127, mapCentral);
        pc17_img = getImage(getCodeBase(), pc17.getImagem());
		btn = new Botao("botao3.gif",600,300,64,48,this);
		btn_img = getImage(getCodeBase(),btn.getImagem());


		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		
		currentState = GameState.stateStoped;
		
	}
	
	public void setImage(Mapeamento m){
		
		map_img = getImage(getCodeBase(),m.getImagem());
		
	}
	
	public void start()
	{
		Thread th = new Thread(this);
		th.start();
	}
	
	public void stop()
	{
		
	}
	
	public void destroy()
	{
		
	}
	
	public void mouseDragged(MouseEvent e)
	{

	}
	
	public void mouseExited(MouseEvent e)
	{

	}
	
	public void mouseMoved(MouseEvent e)
	{
		
	}
	
	public void mouseEntered(MouseEvent e)
	{
		
	}
	
	public void mouseClicked(MouseEvent e)
	{
		
	}
	
	public void mouseReleased(MouseEvent e)
	{

	}
	
	public void mousePressed(MouseEvent e)
	{
		btn.mousePressed(e);
		switch(currentState)
		{
		case statePlaying:
		{
			pc1.mousePressed(e);
			pc2.mousePressed(e);
			pc3.mousePressed(e);
			pc4.mousePressed(e);
			pc5.mousePressed(e);
			pc6.mousePressed(e);
			pc7.mousePressed(e);
			pc8.mousePressed(e);
			pc9.mousePressed(e);
			pc10.mousePressed(e);
			pc11.mousePressed(e);
			pc12.mousePressed(e);
			pc13.mousePressed(e);
			pc14.mousePressed(e);
			pc15.mousePressed(e);
			pc16.mousePressed(e);
			pc17.mousePressed(e);
		}
		break;
		}
	}
	
	public void update(Graphics g)
	{
		if(dbImage == null)
		{
			dbImage = createImage(this.getSize().width, this.getSize().height);
			dbg = dbImage.getGraphics();
		}
		
		dbg.setColor(getBackground());
		dbg.fillRect(0,0,this.getSize().width,this.getSize().height);

		dbg.setColor(getForeground());
		paint(dbg);
		
		g.drawImage(dbImage, 0, 0, this);
	}
	
	public void run()
	{
		//Baixa prioridade para o Thread
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
			
		//Roda um loop infinito
		while (true)
		{
			//Adicionar lógica aqui
			
			repaint();
			try
			{
				Thread.sleep(20);
			}
			catch (InterruptedException ex)
			{
				//FAZ NADA!!
			}
			
			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		}
	}
	
	public void paint(Graphics g)
	{
		//Adicionar elementos a serem pintados
		g.drawImage(bGround,0,0,this);
		g.drawImage(map_img,mapCentral.getX(),mapCentral.getY(),this);
		g.drawImage(pc1_img,pc1.getX(),pc1.getY(),this);
		g.drawImage(pc2_img,pc2.getX(),pc2.getY(),this);
		g.drawImage(pc3_img,pc3.getX(),pc3.getY(),this);
		g.drawImage(pc4_img,pc4.getX(),pc4.getY(),this);
		g.drawImage(pc5_img,pc5.getX(),pc5.getY(),this);
		g.drawImage(pc6_img,pc6.getX(),pc6.getY(),this);
		g.drawImage(pc7_img,pc7.getX(),pc7.getY(),this);
		g.drawImage(pc8_img,pc8.getX(),pc8.getY(),this);
		g.drawImage(pc9_img,pc9.getX(),pc9.getY(),this);
		g.drawImage(pc10_img,pc10.getX(),pc10.getY(),this);
		g.drawImage(pc11_img,pc11.getX(),pc11.getY(),this);
		g.drawImage(pc12_img,pc12.getX(),pc12.getY(),this);
		g.drawImage(pc13_img,pc13.getX(),pc13.getY(),this);
		g.drawImage(pc14_img,pc14.getX(),pc14.getY(),this);
		g.drawImage(pc15_img,pc15.getX(),pc15.getY(),this);
		g.drawImage(pc16_img,pc16.getX(),pc16.getY(),this);
		g.drawImage(pc17_img,pc17.getX(),pc17.getY(),this);
		g.drawImage(btn_img,btn.getX(),btn.getY(),this);
		
		
	}
}
